package com.regtwo.admin.mplayer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MusicService extends Service {
    public static String[]  strs = {"first","second","third"};
    private static MediaPlayer player;
    public String songName;
    public int songNum = 0;
    private String parentPath="/mnt/sdcard/mp";
    File file ;
    String str[] ;
    private int is_paused = 0;
    private Context cxt;
    static Handler handler ;


    private  int currentPos;

    public MusicService() {

        cxt = getApplication();
        handler = MainActivity.handler;
        Log.i("xxx","hanlder:"+handler);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = new MediaPlayer();

    }

    static class MusicThread {
        public static void startThread(){
            new Thread(){
                Message msg = new Message();
                Bundle bundle = new Bundle();
                @Override
                public void run() {
                    while(true){
                        try {
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        bundle.putInt("position",player.getCurrentPosition());
                        msg.setData(bundle);
                        Log.i("bundle","bundle......."+bundle.getInt("position"));
                        handler.sendMessage(msg);
                        if(player.getCurrentPosition() == player.getDuration()){
                            break;
                        }
                    }

                }
            }.start();
        }

    }


    class MyBinder extends Binder{
        /*public int getCurrentProcess(){
            return player.getCurrentPosition();
        }*/
        public int getCurrentProcess(){
            return currentPos;
        }
        public int getSongLong(){
            return player.getDuration();
        }
        public MediaPlayer getPlayer(){
            return player;
        }
        public void set_is_paused(int is_pause){is_paused = is_pause;}
        //public Handler getHandler(){return handler;}
        //public Message getMessage(){return  msg;}
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        String action = intent.getStringExtra("action");

        if(action == null){
            return super.onStartCommand(intent, flags, startId);
        }
        Log.i("gtq-1","action:"+action);
        String tmpcode = null;
        for( int i = 0; i < action.length() ; i++){
            if( action.charAt(i) >='a' && action.charAt(i) <= 'z'){
                tmpcode = tmpcode+action.charAt(i);
            }
        }
        String code = tmpcode.substring(4);
        Log.i("gtq-1","code:......"+code);
        if( code.length() != action.length()){
            songNum = Integer.parseInt(action.substring(code.length()));
        }
        Log.i("gtq-1", "code:" + code + "songNum:" + songNum);
        if( code.equals("start")){
            start();
        }else if( code.equals("prev")){
            prev();
        }else if( code.equals("next")){
            next();
        }else if( code.equals("pause")){
            pause();
        }


        return super.onStartCommand(intent, flags, startId);
    }

    public void start(){
        if(is_paused == 1){
            Log.i("sp","is_paused1 :"+is_paused );
            player.start();
            Log.i("sp", "is_paused2 :" + is_paused);
            is_paused=0;
        }else{
            Log.i("gtq-1","songNum:"+songNum+"............");
            String path = parentPath+"/"+strs[songNum]+".mp3";
            Log.i("gtq-2", "songNum:" + path + "............path");
            player.reset();
            try {
                player.setDataSource(path);
                player.prepare();
            }catch (IOException e){
                e.printStackTrace();
            }
            player.start();
        }


        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                next();
            }
        });
    }
    public void prev(){
        songNum = songNum == 0? strs.length-1:songNum-1;
        start();
    }
    public void next(){
        songNum = songNum== strs.length-1? 0 : songNum+1;
        start();
    }
    public void pause(){
        if( player.isPlaying()){
            player.pause();
        }

    }

    @Override
    public IBinder onBind(Intent intent) {

        return new MyBinder();
    }


}
